package com.party.mobile.biz.analysis;

import com.google.common.collect.Lists;
import com.party.common.utils.StringUtils;
import com.party.core.model.analysis.StatisticsMobileEventFlow;
import com.party.core.model.analysis.StatisticsMobileUser;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.analysis.IStatisticsMobileEventFlowService;
import com.party.core.service.analysis.IStatisticsMobileUserService;
import com.party.core.service.member.IMemberGroupService;
import com.party.mobile.web.dto.analysis.AppEventFlowInput;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * app事件统计
 *
 * @author yifeng
 * @date 2018/9/13 0013
 * @time 下午 14:53
 */
@Service
public class StatisticalAppEventBizService {

    @Autowired
    private IStatisticsMobileEventFlowService statisticsMobileEventFlowService;

    @Autowired
    private IStatisticsMobileUserService statisticsMobileUserService;

    @Autowired
    private IMemberGroupService memberGroupService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 生成用户id
     *
     * @param deviceId  设备id
     * @param partnerId 机构id
     * @return 用户id
     */
    public String getUserId(String deviceId, String partnerId) {
        StringBuilder sb = new StringBuilder();
        sb.append(deviceId);
        sb.append(partnerId);
        String encodeStr = DigestUtils.md5Hex(sb.toString() + "txz123");
        List<String> strings = Lists.newArrayList();
        for (int i = 0; i < encodeStr.length(); i++) {
            char c = encodeStr.charAt(i);
            if (!Character.isDigit(c)) {
                int tempNum = (int) c;
                strings.add(tempNum + "");
            } else {
                strings.add(c + "");
            }
        }
        Collections.reverse(strings);
        String newStr = StringUtils.join(strings.toArray(), "");
        String uniqueCode = newStr.substring(0, 18);
        return uniqueCode;
    }

    /**
     * 事件插入
     *
     * @param eventId   事件id
     * @param flowInput 请求参数
     */
    public void innerEventFlowDataReport(String eventId, AppEventFlowInput flowInput) throws Exception {
        String userId = insertUser(flowInput);
        if (StringUtils.isEmpty(userId)) {
            throw new Exception("userId不能为空");
        }
        StatisticsMobileEventFlow mobileEventFlow = new StatisticsMobileEventFlow();
        mobileEventFlow.setUserId(userId);
        mobileEventFlow.setEventId(eventId);
        mobileEventFlow.setPartnerId(flowInput.getPartnerId());
        statisticsMobileEventFlowService.insert(mobileEventFlow);
    }

    public String insertUser(AppEventFlowInput flowInput) {
        String partnerId = flowInput.getPartnerId();
        String userId = getUserId(flowInput.getDeviceId(), partnerId);
        try {
            MemberGroup partner = memberGroupService.get(partnerId);
            StatisticsMobileUser statisticsUser = new StatisticsMobileUser();
            statisticsUser.setUserId(userId);
            statisticsUser.setMemberId(partnerId);
            statisticsUser.setLogo(partner.getLogo());
            statisticsUser.setName(partner.getRealname());
            statisticsMobileUserService.insert(statisticsUser);
        } catch (Exception e) {
            logger.error("记录用户异常", e);
        }
        return userId;
    }
}
